package controllers

import (
	"beego_benchu/consts"
	redisClient "beego_benchu/services/redis"
	"fmt"

	"github.com/beego/beego/v2/server/web"
	"github.com/garyburd/redigo/redis"
)

type RedisDemoController struct {
	web.Controller
}

func (this *RedisDemoController) GetRedisKey() {
	fmt.Println("111111111111")
	c := redisClient.PoolConnect()
	defer c.Close()

	//写入数据
	_, err := c.Do("SET", "go_key", "redigo")
	if err != nil {
		// 设置过期时间
		c.Do("expire", "go_key", 1000)
		fmt.Println("err while setting:", err)
	}

	//获取value并转成字符串
	r, err := redis.String(c.Do("GET", "go_key"))
	if err != nil {
		fmt.Println("err while getting:", err)
	}
	fmt.Println(r)

	this.Data["json"] = consts.ResultsStruct{
		Code: "200",
		Msg:  "ok",
		Data: r,
	}
	this.ServeJSON()

}
